﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Travianer.OleDbService
{
    public class ServiceBase
    {
        /// <summary>
        /// 获取字符串型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected string GetString(object data)
        {
            if (data == DBNull.Value)
                return "";
            else
                return data.ToString();
        }

        /// <summary>
        /// 获取字符串布尔型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected bool GetBoolean(object data)
        {
            if (data == DBNull.Value)
                return false;
            else
                return Convert.ToBoolean(data);
        }

        /// <summary>
        /// 获取字符串布尔型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected bool? GetBooleanNull(object data)
        {
            if (data == DBNull.Value)
                return null;
            else
                return Convert.ToBoolean(data);
        }

        /// <summary>
        /// 获取整型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected int GetInt32(object data)
        {
            if (data == DBNull.Value)
                return 0;
            else
                return Convert.ToInt32(data);
        }

        /// <summary>
        /// 获取整型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected int? GetInt32Null(object data)
        {
            if (data == DBNull.Value)
                return null;
            else
                return Convert.ToInt32(data);
        }

        /// <summary>
        /// 获取小数类型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected decimal GetDecimal(object data)
        {
            if (data == DBNull.Value)
                return 0;
            else
                return Convert.ToDecimal(data);
        }

        /// <summary>
        /// 获取小数类型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected decimal? GetDecimalNull(object data)
        {
            if (data == DBNull.Value)
                return null;
            else
                return Convert.ToDecimal(data);
        }

        /// <summary>
        /// 获取日期型数据
        /// </summary>
        /// <param name="data">数据</param>
        protected DateTime? GetDateTime(object data)
        {
            if (data == DBNull.Value || Convert.ToDateTime(data).ToString("yyyy-MM-dd") == "1900-01-01")
                return null;
            else
                return Convert.ToDateTime(data);
        }

        /// <summary>
        /// 获取Guid数据
        /// </summary>
        /// <param name="data">数据</param>
        protected Guid GetGuid(object data)
        {
            if (data == DBNull.Value)
                return Guid.Empty;
            else
                return new Guid(data.ToString());
        }
    }
}
